/* @(#)GSAccountInputExeAction.java   
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li    2006-4-20    新增
 *   Jenny Li    2006-6-21    修改取得手动设定参数类型日期区间显示处理方式
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSBalanceParamBean;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSOrganizationBean;
import com.hitrust.gs.business.GSBalanceProcessor;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrganizationProcessor;
import com.hitrust.gs.dao.GSBalanceParamDAO;
import com.hitrust.gs.form.GSBalanceInfoRecordeForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 * 
 */
public class GSBalanceInfoRecordExeAction extends GSBaseAction {

    /* (non-Javadoc)
     * @see com.hitrust.gs.service.GSBaseAction#execute(com.hitrust.gs.service.GSBaseForm)
     */
    public String execute(GSBaseForm aForm) throws Exception {
        GSBalanceInfoRecordeForm tForm = (GSBalanceInfoRecordeForm)aForm;
        try{
          //取得当前操作员可查询的订货单位
            GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
            GSOrganizationProcessor orgpro=new GSOrganizationProcessor();
            ArrayList arr=orgpro.getOrgSub(tOperBean.getGs_oper_org_id());
            ArrayList ary=new ArrayList();
            for (int i = 0; i < arr.size(); i++) {
                GSOrganizationBean ob=(GSOrganizationBean) arr.get(i);
                String[] tOrderDept = new String[2];
                tOrderDept[0]=ob.getGs_org_id();
                tOrderDept[1]=ob.getGs_org_name();
                ary.add(tOrderDept);
            }
            aForm.getRequest().setAttribute("shiporg", ary);
            //取得系统设定的结算日期
            GSBalanceParamDAO tBalParmDAO = new GSBalanceParamDAO();
            GSBalanceParamBean tBean = tBalParmDAO.queryBalanceDate();
            String tCurDay = DateUtil.getCurrentDate("yyyyMMdd");
            if(tBean.getGs_balance_date_type().equals(GSBaseConstants.NATURE_BALANCE_DATE)){//自然月参数类型
                int maxday = DateUtil.getMaxMonthDay(tCurDay);
                tBean.setGs_balance_start_date("1");
                tBean.setGs_balance_end_date(String.valueOf(maxday));
                aForm.getRequest().setAttribute("balancestrdate",DateUtil.formateDateTimeForUser(DateUtil.getSpecDay(1)));
                aForm.getRequest().setAttribute("balanceenddate",DateUtil.formateDateTimeForUser(DateUtil.getSpecDay(maxday)));
            }else{//手动设定参数类型
                int tStrDay = Integer.parseInt(tBean.getGs_balance_start_date());
                int tEndDay = Integer.parseInt(tBean.getGs_balance_end_date());
                int curYear = Integer.parseInt(tCurDay.substring(0,4));
                int curMonth = Integer.parseInt(tCurDay.substring(4,6));
                if(tStrDay > tEndDay || tStrDay == tEndDay){    //开始日期大于等于结束日期
                    if(tCurDay.substring(4,6).equals("01")){    //如果当前月份为1月,则开始区间的年为上一年份
                        aForm.getRequest().setAttribute("balancestrdate",DateUtil.getSpecDate(curYear-1,12,tStrDay,"yyyy/MM/dd"));    
                        aForm.getRequest().setAttribute("balanceenddate",DateUtil.getSpecDate(curYear,1,tEndDay,"yyyy/MM/dd"));
                    }else {
                        aForm.getRequest().setAttribute("balancestrdate",DateUtil.getSpecDate(curYear,curMonth - 1,tStrDay,"yyyy/MM/dd"));    
                        aForm.getRequest().setAttribute("balanceenddate",DateUtil.getSpecDate(curYear,curMonth,tEndDay,"yyyy/MM/dd"));
                    }  
                }else if(tStrDay < tEndDay){        //开始日期小于结束日期
                    aForm.getRequest().setAttribute("balancestrdate",DateUtil.getSpecDate(curYear,curMonth,tStrDay,"yyyy/MM/dd"));    
                    aForm.getRequest().setAttribute("balanceenddate",DateUtil.getSpecDate(curYear,curMonth,tEndDay,"yyyy/MM/dd"));
                }
            }
            aForm.getRequest().setAttribute("balanceparm",tBean);
            
            GSBalanceProcessor tBalProc = new GSBalanceProcessor();
            //执行结算导入
            //boolean tFlag = tBalProc.balanceInfoRecord(tForm);
            String tpexcp_msg="";
            boolean tFlag = false;
            try{
            tFlag = tBalProc.balanceInfoRecordSimple(tForm);
            }
            catch(GSException ge){
                tpexcp_msg=ge.getMessage();
            }
            if(tFlag){
                aForm.getRequest().setAttribute("message","对帐数据生成成功");
            }else{
                aForm.getRequest().setAttribute("message","对帐数据生成失败:"+tpexcp_msg);
            }
        }catch(GSException ge){
            aForm.getRequest().setAttribute("action","A180101.do");
            throw ge;
        }catch(Exception e){
            aForm.getRequest().setAttribute("action","A180101.do");
            throw new GSException(e.toString());
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }

}
